package vumt.radiocountries.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import vumt.radiocountries.R;
import vumt.radiocountries.SplashActivity;
import vumt.radiocountries.object.ChannelObject;
import android.util.Log;
import android.widget.Toast;

public class ChannelParser {

	// từ url xml lấy về stream
	public InputStream getStreamFromUrl(String url) {
		InputStream inputStream = null;

		try {
			HttpGet httpGet = new HttpGet(url);
			HttpParams httpParameters = new BasicHttpParams();
			int timeoutConnection = 50000;
			HttpConnectionParams.setConnectionTimeout(httpParameters,
					timeoutConnection);
			int timeoutSocket = 10000;
			HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
			DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);

			HttpResponse httpResponse = httpClient.execute(httpGet);
			HttpEntity httpEntity = httpResponse.getEntity();
			inputStream = httpEntity.getContent();

		} catch (UnsupportedEncodingException e) {
			Log.d("error_mang", "loi1: " + e.toString());
		} catch (ClientProtocolException e) {
			Log.d("error_mang", "loi2: " + e.toString());
		} catch (IOException e) {
			Log.d("error_mang", "loi3: " + e.toString()); // Loi socket
			Global.INDEX_ERROR_INTERNET = 1;
		} catch (Exception e) {
			Log.d("error_mang", "loi4: " + e.toString());
		}
		return inputStream;
	}

	// chuyển stream -> string
	public String convertStreamToString(InputStream is) throws Exception {
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		StringBuilder sb = new StringBuilder();
		String line = null;

		while ((line = reader.readLine()) != null) {
			sb.append(line);
		}
		is.close();
		return sb.toString();
	}

	// Lấy về list các channel
	public ArrayList<ChannelObject> getChannelList(String xml) {
		ArrayList<ChannelObject> channelsList = new ArrayList<ChannelObject>();
		try {
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser sp = spf.newSAXParser();
			XMLReader xr = sp.getXMLReader();

			ChannelHandler handle = new ChannelHandler();
			xr.setContentHandler(handle);
			InputSource inStream = new InputSource();
			inStream.setCharacterStream(new StringReader(xml));

			xr.parse(inStream);
			channelsList = handle.getChannelList();
		} catch (Exception e) {
		}
		return channelsList;
	}
}
